import { useState, useEffect } from 'react'
import { HashRouter as Router, Routes, Route } from 'react-router-dom'
import withNavigate from './router/withNavigate'
import request from "@services/request";
import routes from './router/index'

function App() {
  const [isLogin, setIsLogin] = useState(false)
  const [urlCode, setUrlCode] = useState('')
  const [state, setState] = useState({
    videoName: '5a721cfc1a0a90205f5ff5784e31d587.mp4',
    listGoods: [
      {
        id: '1',
        name: '佛跳墙',
        detail: '这是佛跳墙',
        amount: '100',
        picName: '20250228133752.png'
      },
      {
        id: '2',
        name: '虫草',
        detail: '这是虫草',
        amount: '300',
        picName: '20250228133758.png'
      }
    ],
    bPrice: 0.1,
    address: 'bF9LW2JqvGaakaeK2eaGPFpdRi6nrHx4e8',
    inviteUserAddress: '',
    addressTwo: '0xc205a554FA64a10764871E9DAFe32D4B93540372',
    amount: '10000',
    balanceNana: 0,
    balanceUsdt: 0,
    withdrawMax: 1000,
    withdrawMin: 0,
    withdrawMaxNa: 1000,
    withdrawMinNa: 1,
    amountUsdtGet: 0,
    amountUsdtSubGet: 0,
    amountUsdtSubGetToday: 0,
    amountUsdt: 0,
    rewardThree: 0,
    amountUsdtSubGetAll: 0,
    teamAll: 0,
    listEth: [],
    listAddress: [
      {
        id: '1',
        phone: '15727370022',
        a: '北京市',
        b: '北京市',
        c: '朝阳区',
        d: 'dassseee',
        status: '1',
        name: 'linhang',
        createdAt: '2025-02-28 19:53:26'
      }
    ],
    listBuy: [
      {
        amount: '123.44',
        createdAt: '2025-02-28',
        goodId: '1',
        addressId: '34444444343434',
        status: '1'
      }
    ],
    listExchange: [],
    listReward: [
      {
        amountUsdt: '123.44',
        amountNa: '44.22',
        rewardType: '2',
        createdAt: '2025-02-28'
      }
    ]
  })
  const [recommends, setRecommends] = useState({})

  useEffect(() => {
    isLogin && getData()
  }, [isLogin])
  const getData = async () => {
    try {
      const res = await request.get("app_server/user_recommend", {
        params: {
          address: state.address
        }
      });
      console.log('res', res)
      setRecommends(res)
    } catch (error) {
      console.log(error)
    }
  }

  const dispatch = {
    setIsLogin,
    setUrlCode
  }

  return (
    <Router>
      <Routes>
        {routes.map((route, index) => {
          const RouteComponent = withNavigate(route.element)

          return (
            <Route
              key={index}
              path={route.path}
              element={
                <RouteComponent
                  {...route}
                  state={state}
                  recommends={recommends}
                  setState={setState}
                  isLogin={isLogin}
                  urlCode={urlCode}
                  dispatch={dispatch}
                />
              }
            />
          )
        })}
      </Routes>
    </Router>
  )
}

export default App
